package org.example.managementsystem.controller;

import org.example.managementsystem.model.Material;
import org.example.managementsystem.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 物料信息控制器
 */
@RestController
@RequestMapping("/api/materials")
@CrossOrigin(origins = "*")
public class MaterialController {
    
    private final MaterialService materialService;

    @Autowired
    public MaterialController(MaterialService materialService) {
        this.materialService = materialService;
    }

    /**
     * 获取所有物料信息
     */
    @GetMapping
    public ResponseEntity<List<Material>> getAllMaterials() {
        List<Material> materials = materialService.getAllMaterials();
        return ResponseEntity.ok(materials);
    }

    /**
     * 根据ID获取物料信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<Material> getMaterialById(@PathVariable Long id) {
        Material material = materialService.getMaterialById(id);
        if (material != null) {
            return ResponseEntity.ok(material);
        }
        return ResponseEntity.notFound().build();
    }

    /**
     * 根据供货商查询物料
     */
    @GetMapping("/provider/{providerName}")
    public ResponseEntity<List<Material>> getMaterialsByProvider(@PathVariable String providerName) {
        List<Material> materials = materialService.getMaterialsByProvider(providerName);
        return ResponseEntity.ok(materials);
    }

    /**
     * 添加物料
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addMaterial(@RequestBody Material material) {
        Material savedMaterial = materialService.addMaterial(material);
        Map<String, Object> response = new HashMap<>();
        response.put("id", savedMaterial.getId());
        response.put("message", "物料添加成功");
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新物料信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, String>> updateMaterial(@PathVariable Long id, @RequestBody Material material) {
        material.setId(id);
        boolean updated = materialService.updateMaterial(material);
        
        Map<String, String> response = new HashMap<>();
        if (updated) {
            response.put("message", "物料信息更新成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "物料信息更新失败，未找到指定ID的物料");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 删除物料
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, String>> deleteMaterial(@PathVariable Long id) {
        boolean deleted = materialService.deleteMaterial(id);
        
        Map<String, String> response = new HashMap<>();
        if (deleted) {
            response.put("message", "物料删除成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "物料删除失败，未找到指定ID的物料");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 搜索物料
     */
    @GetMapping("/search")
    public ResponseEntity<List<Material>> searchMaterials(@RequestParam String keyword) {
        List<Material> materials = materialService.searchMaterials(keyword);
        return ResponseEntity.ok(materials);
    }
}
